package at.rtm.web;

import at.rtm.dao.HRDTemperatureDAOImpl;
import at.rtm.domain.Temperature;
import at.rtm.service.extractor.SWZWaterTemperatureExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

@Controller
public class SchedulingController {
    private static final Logger log = Logger.getLogger(SchedulingController.class.getName());

    private final AtomicInteger counter = new AtomicInteger();

    @Autowired
    SWZWaterTemperatureExtractor swzTemperatureExtractor;

    @Autowired
    HRDTemperatureDAOImpl temperatureDataDAO;

    @RequestMapping(value = "scheduler.html")
    public String process(Model model) {
        Double temperature = 0.0;
        try {
            temperature = swzTemperatureExtractor.extractInformation(new UrlResource("http://www.swz.at/region_salzkammergut/lake_attersee/"), Double.class);
            temperatureDataDAO.save(new Temperature(new GregorianCalendar(), temperature));
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        System.out.println("processing next 10");
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("rudolf.traunmueller@maat.at", "Rudolf Traunmueller"));
            msg.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("rudolf.traunmueller+appengine@maat.at", "rtm"));
            msg.setSubject("Aktuelle Wassertemperatur Attersee " + temperature + " Grad");
            StringBuffer sb = new StringBuffer();
            sb.append("********************************************************************\n");
            sb.append("* Aktuelle Wassertemperatur Attersee " + temperature + " Grad\n");
            sb.append("********************************************************************\n");

            List<Temperature> temperatures = temperatureDataDAO.findAll();
            for (Temperature temperatureItem : temperatures) {
                sb.append("* " + temperatureItem.getCal().getTime() + " / " + temperatureItem.getTemperature() + " Grad\n");
            }
            sb.append("********************************************************************\n");
            msg.setText(sb.toString());
            System.out.println(sb.toString());
            Transport.send(msg);

        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "redirect:index.html";
    }
}